.TH std::basic_regex::basic_regex 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_regex::basic_regex \- std::basic_regex::basic_regex

.SH Synopsis
   basic_regex();                                                     \fB(1)\fP \fI(since C++11)\fP
   explicit basic_regex( const CharT* s,
                         flag_type f =                                \fB(2)\fP \fI(since C++11)\fP
   std::regex_constants::ECMAScript );
   basic_regex( const CharT* s, std::size_t count,                    \fB(3)\fP \fI(since C++11)\fP
                flag_type f = std::regex_constants::ECMAScript );
   basic_regex( const basic_regex& other );                           \fB(4)\fP \fI(since C++11)\fP
   basic_regex( basic_regex&& other ) noexcept;                       \fB(5)\fP \fI(since C++11)\fP
   template< class ST, class SA >

   explicit basic_regex( const std::basic_string<CharT,ST,SA>& str,   \fB(6)\fP \fI(since C++11)\fP

                         flag_type f =
   std::regex_constants::ECMAScript );
   template< class ForwardIt >

   basic_regex( ForwardIt first, ForwardIt last,                      \fB(7)\fP \fI(since C++11)\fP

                flag_type f = std::regex_constants::ECMAScript );
   basic_regex( std::initializer_list<CharT> init,                    \fB(8)\fP \fI(since C++11)\fP
                flag_type f = std::regex_constants::ECMAScript );

   Constructs a new regular expression from a sequence of characters interpreted
   according to the flags f.

   1) Default constructor. Constructs an empty regular expression which will match
   nothing.
   2) Constructs a regex from a null-terminated string s.
   3) Constructs a regex from a sequence of count characters, pointed to by s.
   4) Copy constructor. Constructs a regex by copying other.
   5) Move constructor. Constructs a regex with the contents of other using move
   semantics.
   6) Constructs a regex from a string str.
   7) Range constructor. Constructs the string with the contents of the range
   [first, last).
   8) Initializer list constructor. Constructs the string with the contents of the
   initializer list init.

.SH Parameters

   s           - pointer to a null-terminated string
   count       - length of a character sequence used to initialize the regex
   first, last - range of a character sequence used to initialize the regex
   str         - a basic_string used as a source used to initialize the regex
   other       - another regex to use as source to initialize the regex
   init        - initializer list used to initialize the regex
   f           - flags used to guide the interpretation of the character sequence as a
                 regular expression
.SH Type requirements
   -
   ForwardIt must meet the requirements of LegacyForwardIterator.

.SH Exceptions

   1) May throw implementation-defined exceptions.
   2,3) std::regex_error if the supplied regular expression is not valid.
   4) May throw implementation-defined exceptions.
   6-8) std::regex_error if the supplied regular expression is not valid.

.SH Example


// Run this code

 #include <iomanip>
 #include <iostream>
 #include <regex>
 #include <string>

 void match_and_print(const std::string& text, const std::regex& pattern)
 {
     std::sregex_iterator it(text.begin(), text.end(), pattern), it_end;
     int count = 0;
     for (; it != it_end; ++it)
     {
         const std::smatch& match = *it;
         std::cout << ++count << ". " << std::quoted(match.str()) << '\\n';
     }
     std::cout << (count ? "\\n" : "no match found\\n\\n");
 }

 int main()
 {
     const std::string text = "Hello, World! 12345";

     // Matches one or more digits
     std::string pattern_text = "\\\\d+";
     std::cout << "digits (" << pattern_text << "):\\n";
     auto pattern = std::regex(pattern_text);
     match_and_print(text, pattern);

     // Matches one or more characters split by space
     pattern_text = "[^\\\\s]+";
     std::cout << "words (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text);
     match_and_print(text, pattern);

     // Matches one or more characters split by space
     pattern_text = "[a-zA-Z]+";
     std::cout << "words without symbols and digits (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text);
     match_and_print(text, pattern);

     // Matches one non digits, non alphabet
     pattern_text = "[^0-9A-Za-z]";
     std::cout << "symbol (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text);
     match_and_print(text, pattern);

     // Matches one or more lowercase
     pattern_text = "[a-z]+";
     std::cout << "lowercase (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text);
     match_and_print(text, pattern);

     // Matches one or more lowercase with std::regex::icase flag
     pattern_text = "[a-z]+";
     std::cout << "lowercase with ignore case flag (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text, std::regex::icase);
     match_and_print(text, pattern);

     // Matches basic POSIX regular expression
     pattern_text = "[[:digit:]]+";
     std::cout << "basic POSIX regex (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text, std::regex::basic);
     match_and_print(text, pattern);

     // Matches extended POSIX regular expression
     pattern_text = "[[:digit:]]+";
     std::cout << "extended POSIX regex (" << pattern_text << "):\\n";
     pattern = std::regex(pattern_text, std::regex::extended);
     match_and_print(text, pattern);
 }

.SH Output:

 digits (\\d+):
 1. "12345"

 words ([^\\s]+):
 1. "Hello,"
 2. "World!"
 3. "12345"

 words without symbols and digits ([a-zA-Z]+):
 1. "Hello"
 2. "World"

 symbol ([^0-9A-Za-z]):
 1. ","
 2. " "
 3. "!"
 4. " "

 lowercase ([a-z]+):
 1. "ello"
 2. "orld"

 lowercase with ignore case flag ([a-z]+):
 1. "Hello"
 2. "World"

 basic POSIX regex ([[:digit:]]+):
 no match found

 extended POSIX regex ([[:digit:]]+):
 1. "12345"
